package com.example.lettcode._20241012;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
228. 汇总区间

给定一个  无重复元素 的 有序 整数数组 nums 。
返回 恰好覆盖数组中所有数字 的 最小有序 区间范围列表 。也就是说，nums 的每个元素都恰好被某个区间范围所覆盖，并且不存在属于某个范围但不属于 nums 的数字 x 。
列表中的每个区间范围 [a,b] 应该按如下格式输出：
"a->b" ，如果 a != b
"a" ，如果 a == b

示例 1：
输入：nums = [0,1,2,4,5,7]
输出：["0->2","4->5","7"]
解释：区间范围是：
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"

示例 2：
输入：nums = [0,2,3,4,6,8,9]
输出：["0","2->4","6","8->9"]
解释：区间范围是：
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
 */
public class Qu_jian_hui_zong {

    public static void main(String[] args) {
        int[] nums = {0,1,2,4,5,7};
        summaryRanges3(nums).forEach(System.out::println);
    }

    public static List<String> summaryRanges3(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new ArrayList<>();
        }
        if (nums.length == 1) {
            return Arrays.asList(String.valueOf(nums[0]));
        }

        int start = 0;
        int end = 1;


        List<String> list = new ArrayList<>();
        while (end <= nums.length) {
            if (end == nums.length || nums[end] - nums[start] != end - start) {
                StringBuilder builder = new StringBuilder(String.valueOf(nums[start]));
                if (end - start > 1) {
                    builder.append("->");
                    builder.append(nums[end]);
                }
                list.add(builder.toString());
                start = end;
            }
            end++;
        }
        return list;
    }

    public static List<String> summaryRanges2(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new ArrayList<>();
        }
        if (nums.length == 1) {
            return Arrays.asList(String.valueOf(nums[0]));
        }
        List<String> list = new ArrayList<>();

        StringBuilder builder = new StringBuilder();

        int min = nums[0];
        int max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if (i == nums.length - 1) {
                if (nums[i-1] + 1 == nums[i]) {
                    max = nums[i];
                    if (min == max) {
                        builder.append(min);
                    } else {
                        builder.append(min).append("->").append(max);
                    }
                    list.add(builder.toString());
                } else {
                    list.add(builder.append(min).toString());
                }
            }else {
                if (nums[i] + 1 == nums[i + 1]) {
                    max = nums[i + 1];
                } else {
                    if (min == max) {
                        builder.append(min);
                    } else {
                        builder.append(min).append("->").append(max);
                    }
                    min = nums[i + 1];
                    max = nums[i + 1];
                    list.add(builder.toString());
                    builder = new StringBuilder();
                }
            }
        }
        return list;
    }

    public static List<String> summaryRanges(int[] nums) {
        List<String> list = new ArrayList<>();

        StringBuilder builder = new StringBuilder();

        int min = nums[0];
        int max = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i - 1] + 1 == nums[i]) {
                max = nums[i];
            } else {
                if (min == max) {
                    builder.append(min);
                    list.add(builder.toString());
                } else {
                    builder.append(min).append("->").append(max);
                    list.add(builder.toString());
                }
                if (i != nums.length - 1) {
                    min = nums[i];
                    max = nums[i];
                }
                builder = new StringBuilder();
            }

        }
        return list;
    }

}
